package com.example.project.controller.project;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.IssueCategorie;
import com.example.project.entity.Project;
import com.example.project.enums.BusinessType;
import com.example.project.service.IssueCategorieService;
import com.example.project.service.ProjectService;
import com.example.project.utils.poi.ExcelUtil;

/**
 * 问题分类，定义项目中的问题分类Controller
 * 
 * @author ruoyi
 * @date 2025-10-23
 */
@Controller
@RequestMapping("/system/categorie")
public class IssueCategorieController extends BaseController {
	
	private String prefix = "pm/categorie";

	@Autowired
	private IssueCategorieService isueCategorieService;

	@Autowired
	private ProjectService projectService;

	@RequiresPermissions("system:categorie:view")
	@GetMapping()
	public String categorie(ModelMap mmap) {
		List<Project> projectList = projectService.findAllList(1);
		mmap.put("projectList", projectList);
		return prefix + "/categorie";
	}

	/**
	 * 查询问题分类，定义项目中的问题分类列表
	 */
	@RequiresPermissions("system:categorie:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(IssueCategorie tIssueCategories) {
		startPage();
		List<IssueCategorie> list = isueCategorieService.selectIssueCategorieList(tIssueCategories);

		List<Project> projectList = projectService.findAllList(1);
		Map<Long, String> projectMap = projectList.stream().collect(Collectors.toMap(Project::getId, Project::getName));
		list.forEach(info -> {
			info.setProjectName(projectMap.get(info.getId()));
		});
		return getDataTable(list);
	}

	/**
	 * 导出问题分类，定义项目中的问题分类列表
	 */
	@RequiresPermissions("system:categorie:export")
	@Log(title = "问题分类，定义项目中的问题分类", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(IssueCategorie tIssueCategories) {
		List<IssueCategorie> list = isueCategorieService.selectIssueCategorieList(tIssueCategories);
		ExcelUtil<IssueCategorie> util = new ExcelUtil<IssueCategorie>(IssueCategorie.class);
		return util.exportExcel(list, "问题分类，定义项目中的问题分类数据");
	}

	/**
	 * 新增问题分类，定义项目中的问题分类
	 */
	@RequiresPermissions("system:categorie:add")
	@GetMapping("/add")
	public String add(Long projectId, ModelMap mmap) {
		mmap.put("projectId", projectId);
		if (projectId == null || projectId == 0) {
			List<Project> projectList = projectService.findAllList(1);
			mmap.put("projectList", projectList);
		} else {
			Project project = projectService.selectProjectById(projectId);
			mmap.put("project", project);
		}
		return prefix + "/add";
	}

	/**
	 * 新增保存问题分类，定义项目中的问题分类
	 */
	@RequiresPermissions("system:categorie:add")
	@Log(title = "问题分类，定义项目中的问题分类", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(IssueCategorie issueCategorie) {
		// 检查分类名称是否已存在
		if (isueCategorieService.checkCategoryNameExists(issueCategorie.getProjectId(), issueCategorie.getName())) {
			return error("分类名称" + issueCategorie.getName() + "已存在，请使用其他名称");
		}
		return toAjax(isueCategorieService.insertIssueCategorie(issueCategorie));
	}

	/**
	 * 修改问题分类，定义项目中的问题分类
	 */
	@RequiresPermissions("system:categorie:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		IssueCategorie tIssueCategories = isueCategorieService.selectIssueCategorieById(id);
		mmap.put("tIssueCategories", tIssueCategories);
		return prefix + "/edit";
	}

	/**
	 * 修改保存问题分类，定义项目中的问题分类
	 */
	@RequiresPermissions("system:categorie:edit")
	@Log(title = "问题分类，定义项目中的问题分类", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(IssueCategorie tIssueCategories) {
		return toAjax(isueCategorieService.updateIssueCategorie(tIssueCategories));
	}

	/**
	 * 删除问题分类，定义项目中的问题分类
	 */
	@RequiresPermissions("system:categorie:remove")
	@Log(title = "问题分类，定义项目中的问题分类", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(isueCategorieService.deleteIssueCategorieByIds(ids));
	}
}
